package com.googlecode.connectlet.crypto.digest;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.MacSpi;

public abstract class HmacSpi extends MacSpi {
	protected abstract HmacStream getHmacStream(byte[] key);

	private HmacStream hmac;

	@Override
	protected void engineInit(Key key, AlgorithmParameterSpec paramSpec) {
		hmac = getHmacStream(key.getEncoded());
	}

	@Override
	protected void engineReset() {
		hmac.reset();
	}

	@Override
	protected void engineUpdate(byte b) {
		hmac.write(b);
	}

	@Override
	protected void engineUpdate(byte[] b, int off, int len) {
		hmac.write(b, off, len);
	}

	@Override
	protected byte[] engineDoFinal() {
		return hmac.digest();
	}
}